<?php
/**
 * Source code of PepAdminController class controller
 * @category puntoengine
 * @package servlets
 * @subpackage pepadmin
 * @author Juan Benavides Romero <juan.benavides.romero@gmail.com>
 * @since 0.1.1
 */


/**
 * PepAdminController is a controller class of the default admin
 * @category puntoengine
 * @package servlets
 * @subpackage pepadmin
 * @author Juan Benavides Romero <juan.benavides.romero@gmail.com>
 * @since 0.1.1
 */
class PepAdminController extends HttpServlet {
    protected $master = '/template/pepadmin/adminmaster.php';
    

    /**
     * Implement the GET method connection and load the login form
     * @param HttpRequest $request Page request
     */
    protected function doGet(HttpRequest $request) {
	if($request->getSession('admin') == 'true') {
	    $this->setRequestDispatcher('/template/pepadmin/admin.php', $request);
	} else {
	    $this->setRequestDispatcher('/template/pepadmin/login.php', $request);
	}
    }//doGet


    /**
     * Implement the POST method connection and check the user
     * @param HttpRequest $request Page request
     */
    protected function doPost(HttpRequest $request) {
	$document = new XmlDocument();
	$document->loadXmlFile('/config/admin.xml');

	try {
	    $admin = $document->selectSingleNode('/Admin/Users/User[Credentials/@user="'.$request->getParam('user').'" and Credentials/@pass="'.$request->getParam('pass').'"]');

	    if($admin != '') {
		$request->addSession('admin', 'true');
	    }
	} catch(Exception $ex) {
	}

	$this->sendRedirect('~/'.$this->url);
    }//doPost
}//PepAdminController
?>